/*
 * XTableRenderers.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.table;

import java.awt.Component;
import java.text.NumberFormat;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import org.xfc.XApp;

/**
 * 
 * 
 * @author Devon Carew
 */
public class XTableRenderers
{
	
	private XTableRenderers()
	{
		
	}
	
	public static TableCellRenderer getBooleanRenderer()
	{
		return getBooleanRenderer(false);
	}
	
	public static TableCellRenderer getBooleanRenderer(final boolean rendererFalseValues)
	{
		return new XDefaultTableCellRenderer() {
			Icon selectedIcon_box = XApp.getApp().getResources().getIcon("/eclipse/misc/checkmark_selected.gif");
			Icon unselectedIcon_box = XApp.getApp().getResources().getIcon("/eclipse/misc/checkmark_unselected.gif");
			Icon selectedIcon = XApp.getApp().getResources().getIcon("/eclipse/misc/checkmark.gif");
			
			{
				setHorizontalAlignment(JLabel.CENTER);
			}
			
			protected void decorate(JTable table, Object value, int row, int column)
			{
				if (Boolean.TRUE.equals(value))
				{
					setIcon(rendererFalseValues ? selectedIcon_box : selectedIcon);
				}
				else
				{
					setIcon(rendererFalseValues ? unselectedIcon_box : null);
				}
			}
		};
	}
	
	public static TableCellRenderer getIntegerRenderer()
	{
		return new XNumericRenderer(true);
	}
	
	public static TableCellRenderer getDoubleRenderer()
	{
		return new XNumericRenderer();
	}
	
	public static TableCellRenderer getDoubleRenderer(int minFractionDigits)
	{
		return getDoubleRenderer(minFractionDigits, -1);
	}
	
	public static TableCellRenderer getDoubleRenderer(int minFractionDigits, int maxFractionDigits)
	{
		return new XNumericRenderer(minFractionDigits, maxFractionDigits);
	}
	
	// TODO: printf
	
//	public static TableCellRenderer getScientificRenderer(final boolean forceScientific)
//	{
//		return new XDefaultTableCellRenderer() {
//			
//			PrintfFormat printf;
//			
//			{
//				setHorizontalAlignment(SwingConstants.RIGHT);
//				
//				printf = new PrintfFormat(forceScientific ? "%.4e" : "%.4g");
//			}
//			
//			protected void decorate(JTable table, Object value, int row, int column)
//			{
//				Double dVal = (Double)value;
//				
//				if (dVal != null)
//				{
//					setText(printf.sprintf(dVal.doubleValue()));
//				}
//				else
//				{
//					setText("");
//				}
//			}
//		};
//		
//	}
	
	public static TableCellRenderer getToStringRenderer()
	{
		return getToStringRenderer(SwingConstants.LEFT);
	}
	
	public static TableCellRenderer getToStringRenderer(final int alignment)
	{
		return new XDefaultTableCellRenderer() {
			{ setHorizontalAlignment(alignment); }
			
			protected void decorate(JTable table, Object value, int row, int column)
			{
				setText(value == null ? "" : value.toString());
			}
		};
	}
	
	private static class XDefaultTableCellRenderer
		extends DefaultTableCellRenderer
	{
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, column);
			
			decorate(table, value, row, column);
			
			return this;
		}
		
		protected void decorate(JTable table, Object value, int row, int column)
		{
			
		}
	}
	
	private static class XNumericRenderer
		extends XDefaultTableCellRenderer
	{
		private NumberFormat nf;
		
		{
			setHorizontalAlignment(JLabel.RIGHT);
		}
		
		XNumericRenderer()
		{
			this(false);
		}
		
		XNumericRenderer(boolean integerRenderer)
		{
			if (integerRenderer)
				nf = NumberFormat.getIntegerInstance();
			else
				nf = NumberFormat.getNumberInstance();
		}
		
		XNumericRenderer(int minFractionDigits, int maxFractionDigits)
		{
			nf = NumberFormat.getNumberInstance();
			
			if (minFractionDigits != -1)
				nf.setMinimumFractionDigits(minFractionDigits);
			
			if (maxFractionDigits != -1)
				nf.setMaximumFractionDigits(maxFractionDigits);
		}
		
		protected void decorate(JTable table, Object value, int row, int column)
		{
			if (value != null)
				setText(nf.format(value));
		}
	}
	
}
